package com.example.clq.amap;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AMap.OnMapLongClickListener,PoiSearch.OnPoiSearchListener{

    private MapView mapView;
    private AMap aMap;
    private EditText edtText;
    private Button btnSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mapView.onCreate(savedInstanceState);
        gotoGDCP();
        initSearch();
    }

    private void initViews() {
        mapView = (MapView) findViewById(R.id.mapView);  //获取地图控件引用
        aMap = mapView.getMap();
        edtText = findViewById(R.id.edt_key);
        btnSearch = findViewById(R.id.btn_search);
    }

    private void initSearch() {
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String key = edtText.getText().toString();
                PoiSearch.Query query = new PoiSearch.Query(key,"","020");
                query.setPageNum(0);
                query.setPageSize(10);

                PoiSearch poiSearch  = new PoiSearch(MainActivity.this,query);
                poiSearch.setBound(new PoiSearch.SearchBound
                        (new LatLonPoint(23.18321,113.353111)
                                ,2000));
                poiSearch.setOnPoiSearchListener(MainActivity.this);
                poiSearch.searchPOIAsyn();
            }
        });
    }

    private void gotoGDCP() {
        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition
                (new CameraPosition(new LatLng(23.18321,113.353111),15,30,0));
        aMap.moveCamera(cameraUpdate);
        aMap.setOnMapLongClickListener((AMap.OnMapLongClickListener) this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onMapLongClick(LatLng latLng) {
        Marker marker = aMap.addMarker(new MarkerOptions().position(latLng)
                .title("广交院").snippet("欢迎光临"));
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        Log.e("MainActivity","onPoiSearched");
        if(poiResult == null){
            return;
        }

        ArrayList<PoiItem> poiItems = poiResult.getPois();
        Log.e("MainActivity","poiItems.size:"+poiItems.size());
        for(PoiItem poiItem : poiItems){
            Log.e("MainActivity","onPoiSearched:"+poiItem.getTitle());
            double latitle = poiItem.getLatLonPoint().getLatitude();
            double longitude = poiItem.getLatLonPoint().getLongitude();

            LatLng latLng =new LatLng(latitle,longitude);
            aMap.addMarker(new MarkerOptions().position(latLng).title(poiItem.getTitle()).snippet(poiItem.getTel()));
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }
}
